<?php

namespace App\Model;

use Illuminate\Support\Facades\DB;

class TemplateView extends BaseModel
{
    protected $table = 'template_view';
    private $fields = ['view_id', 'layout_id', 'view_name', 'action', 'view_alias_name', 'type'];


    public function getList()
    {
        return DB::connection('api')->table($this->table)->select($this->fields)->orderBy('type', 'desc')->get();
    }

    public function getListByType($request)
    {
        $where['type'] = $request['type'] ?? 0;
        return DB::connection('api')->table($this->table)->where($where)->select($this->fields)->get();
    }


    public function getTagById($id)
    {
        $query = new self();
        return $query->where('id', '=', $id)->where('status', '=', 1)->select($this->fields)->first();
    }

    public function createPage($data)
    {
        $data['action'] = "kkaudio://template/view?name=" . $data['view_alias_name'];
        return curl_request(getKkaudioApiUrl() . '/inner/template/view/create', $data, 'urlencoded', 'GET');
    }


    public function pageDel($data)
    {
        $d['view_id'] = $data['view_id'];

        return curl_request(getKkaudioApiUrl() . '/inner/template/view/remove', $d, 'urlencoded', 'GET');
    }

    public function previewPage($user_ids = [])
    {
        $data['user_ids'] = implode(',', $user_ids);
        $data['type'] = 1;
        return curl_request(getKkaudioApiUrl() . '/inner/template/view/set/preview', $data, 'urlencoded', 'GET');
    }

    public function getPreviewPage()
    {
        $curl = curl_request(getKkaudioApiUrl() . '/inner/template/view/set/preview', [], 'urlencoded', 'GET');
        $data = [];
        if ($curl['code'] === 0) {
            $where = explode(',', $curl['data']['user_ids']);
            $res = DB::connection('api')->table('user')->whereIn('user_id', $where)->select('user_id', 'uuid', 'nickname', 'portrait')->get();
            $res = json_decode($res, true);
            $data = [];
            foreach ($res as $k => $v) {
                $img = json_decode($v['portrait'],true);
                if (strpos($img[0], 'http:') === false) {
                    $data[$k]['image'] = config('kkaudio.oss_url') . $img[0];
                }
                $data[$k]['id'] =  $v['user_id'];
                $data[$k]['uuid'] =  $v['uuid'];
                $data[$k]['name'] =  $v['nickname'];
            }
        }
        return $data;
    }


    public function issuePage()
    {
        return curl_request(getKkaudioApiUrl() . '/inner/template/view/set/release', [], 'urlencoded', 'GET');
    }

    public function addProportion($input)
    {
        return curl_request(getKkaudioApiUrl(). '/inner/home/addProportion', $input, 'json', 'POST');
    }

    public function editProportion($input)
    {
        return curl_request(getKkaudioApiUrl(). '/inner/home/editProportion', $input, 'json', 'POST');
    }

    //获取首页比例配置
    public function getProportion()
    {
        $res = DB::connection('api')->table('homepage_config')->select('id', 'name', 'channel', 'dec', 'proportion')->orderBy('id', 'desc')->get();
        $res = $this->objToArray($res);
        foreach ($res as $k => $v) {
            $res[$k]['proportion'] = json_decode($v['proportion'], true);
        }
        return $res;
    }

    //删除首页比例配置
    public function delProportion($id)
    {
        return curl_request(getKkaudioApiUrl(). '/inner/home/delProportion', ['id' => $id], 'json', 'POST');
    }

    /**获取渠道id
     * @param $id
     * @return int|mixed
     */
    public function getPropChannel($id)
    {
        $res = DB::connection('api')->table('homepage_config')
            ->where('id',$id)
            ->first();
        return $res->channel ?? 0;
    }

    /**获取所有渠道
     * @return array
     */
    public function getChannels()
    {
        $res = DB::connection('api')->table('homepage_config')->get();
        $data = $this->objToArray($res);
        return empty($data) ? [] : array_column($data ,'channel');
    }
}
